package com.shopping.service.impl;

import com.shopping.bean.Administrator;
import com.shopping.bean.User;
import com.shopping.dao.AdminDao;
import com.shopping.dao.UserDao;
import com.shopping.service.IShoppingLoginService;
import com.shopping.util.SYS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ShoppingLoginService implements IShoppingLoginService {
    @Autowired
    UserDao userDao;
    @Autowired
    AdminDao adminDao;
    @Override
    public User dologin(User user ) {
       User user_data=userDao.getUserByUsername(user.getUsername());
       if (user_data==null){
           throw new RuntimeException("没有此账号");
       }
       if (!SYS.checkPass(user.getPassword(),user_data.getPassword())){
           throw new RuntimeException("密码不正确");
       }
        if (user_data.getState() == 1) {
            throw new RuntimeException("用户已被禁用，无法登录");
        }
       return  user_data;
    }
@Override
    public Administrator systemlogin(Administrator administrator) {
        if (administrator == null) {
            throw new RuntimeException("传入的管理员对象为 null");
        }
        if (adminDao == null) {
            throw new RuntimeException("AdminDao 未正确注入");
        }
        Administrator admin_data = adminDao.getAdminByAdminname(administrator.getAdminname());
        if (admin_data == null) {
            throw new RuntimeException("没有此账号");
        }
        if (admin_data.getAdminpassword() == null) {
            throw new RuntimeException("密码不正确");
        }
        return admin_data;
    }
}

